﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Public;

namespace SalomonWebsite.UC
{
    public partial class CatTree : System.Web.UI.UserControl
    {
        DataTable dtRtn = new DataTable();
        public string str = "";
        public int indexElement = 0;

        #region //封装字段
        private int _parid;

        public int ParPKID
        {
            get { return _parid; }
            set { _parid = value; }
        }
        
        
        private int _pkid;

        public int PKID
        {
            get { return _pkid; }
            set { _pkid = value; }
        }

        private int _faid;

        public int FarPKID
        {
            get { return _faid; }
            set { _faid = value; }
        }

        #endregion

        

        protected void Page_Load(object sender, EventArgs e)
        {
            GetCatList(ParPKID);

            
            if (dtRtn.Rows.Count > 0)
            {
                int indexTmp = 0;
                for (int i = 0; i < dtRtn.Rows.Count; i++)
                {

                    if (Convert.ToInt16(dtRtn.Rows[i]["CatRank"]) == 2) // 2级分类
                    {
                        indexTmp++;
                        if (i != 0)
                        {
                            str += "</ul></li>";
                        }

                        str += "<li>";
                        if (Convert.ToInt32(dtRtn.Rows[i]["PKID"]) == ParPKID || Convert.ToInt32(dtRtn.Rows[i]["PKID"]) == PKID || Convert.ToInt32(dtRtn.Rows[i]["PKID"]) == FarPKID)
                        {
                            str += "<a class=\"ProdNavSideSubject ProdNavSideHasSub ProdNavSideSel\" href=\"javascript:void(0);\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a>";
                            indexElement = indexTmp -1;
                        }
                        else
                        {
                            str += "<a class=\"ProdNavSideSubject ProdNavSideHasSub\" href=\"javascript:void(0);\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a>";
                        }
                        str += "<ul class=\"ProdNavSideContent\">";
                        if (Convert.ToInt32(dtRtn.Rows[i]["Num"]) == 0)
                        {
                            if (Convert.ToInt32(dtRtn.Rows[i]["PKID"]) == ParPKID || Convert.ToInt32(dtRtn.Rows[i]["PKID"]) == PKID || Convert.ToInt32(dtRtn.Rows[i]["PKID"]) == FarPKID)
                            {
                                str += "<li><a href=\"/Ser-" + ParPKID + "-" + Convert.ToInt32(dtRtn.Rows[i]["PKID"]) + "-" + dtRtn.Rows[i]["CatCode"].ToString() + ".aspx\" class=\"ProdNavSideSubSel\">查看所有本分类</a></li>";
                            }
                            else
                            {
                                str += "<li><a href=\"/Ser-" + ParPKID + "-" + Convert.ToInt32(dtRtn.Rows[i]["PKID"]) + "-" + dtRtn.Rows[i]["CatCode"].ToString() + ".aspx\" >查看所有本分类</a></li>";
                            }
                            //str += "</ul></li>";
                        }
                        else
                        {
                            if (Convert.ToInt32(dtRtn.Rows[i]["PKID"]) == ParPKID || Convert.ToInt32(dtRtn.Rows[i]["PKID"]) == PKID || Convert.ToInt32(dtRtn.Rows[i]["PKID"]) == FarPKID)
                            {
                                str += "<li><a href=\"/Cat-" + ParPKID + "-" + Convert.ToInt32(dtRtn.Rows[i]["PKID"]) + "-" + dtRtn.Rows[i]["CatCode"].ToString() + ".aspx\" class=\"ProdNavSideSubSel\">查看所有本分类</a></li>";
                            }
                            else
                            {
                                str += "<li><a href=\"/Cat-" + ParPKID + "-" + Convert.ToInt32(dtRtn.Rows[i]["PKID"]) + "-" + dtRtn.Rows[i]["CatCode"].ToString() + ".aspx\" >查看所有本分类</a></li>";
                            }
                            
                        }
                    }
                    else
                    {
                        str += "<li><a href=\"/Ser-" + ParPKID + "-" + Convert.ToInt32(dtRtn.Rows[i]["PKID"]) + "-" + dtRtn.Rows[i]["CatCode"].ToString() + ".aspx\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></li>";
                    }

                    if (i == dtRtn.Rows.Count - 1)
                    {
                        str += "</ul></li>";
                    }
                }
            }
            else
            {
                str += "<li>暂无分类</li>";
            }
        }

        protected void GetCatList(int ParID)
        {
            DataTable dt = Bll.Category.MULTIPLE(Global.Connection);
            if (dt.Rows.Count > 0)
            {

                // Add three column objects to the table.
                DataColumn dc1 = new DataColumn();
                dc1.DataType = System.Type.GetType("System.Int32");
                dc1.ColumnName = "PKID";
                //dc1.AutoIncrement = true;
                dtRtn.Columns.Add(dc1);

                DataColumn dc2 = new DataColumn();
                dc2.DataType = System.Type.GetType("System.String");
                dc2.ColumnName = "CatName";
                //dc2.AutoIncrement = true;
                dtRtn.Columns.Add(dc2);



                DataColumn dc3 = new DataColumn();
                dc3.DataType = System.Type.GetType("System.Int32");
                dc3.ColumnName = "ParID";
                //dc3.AutoIncrement = true;
                dtRtn.Columns.Add(dc3);

                DataColumn dc4 = new DataColumn();
                dc4.DataType = System.Type.GetType("System.Int32");
                dc4.ColumnName = "CatRank";
                //dc4.AutoIncrement = true;
                dtRtn.Columns.Add(dc4);

                DataColumn dc5 = new DataColumn();
                dc5.DataType = System.Type.GetType("System.Int32");
                dc5.ColumnName = "SortBy";
                //dc5.AutoIncrement = true;
                dtRtn.Columns.Add(dc5);

                DataColumn dc6 = new DataColumn();
                dc6.DataType = System.Type.GetType("System.Int32");
                dc6.ColumnName = "Num";
                //dc5.AutoIncrement = true;
                dtRtn.Columns.Add(dc6);


                DataColumn dc7 = new DataColumn();
                dc7.DataType = System.Type.GetType("System.String");
                dc7.ColumnName = "CatCode";
                //dc2.AutoIncrement = true;
                dtRtn.Columns.Add(dc7);

                GetCatData(ParID, dt, dtRtn);

                //if (dtRtn.Rows.Count > 0)
                //{

                //}

            }
        }


        protected void GetCatData(int parID, DataTable dtCat, DataTable dtRtn)
        {
            DataRow[] drs = dtCat.Select(" [ParID] = " + parID);

            for (int i = 0; i < drs.Length; i++)
            {
                DataRow dr = dtRtn.NewRow();
                dr["PKID"] = Convert.ToInt32(drs[i]["PKID"]);
                dr["CatName"] = drs[i]["CatName"].ToString();
                dr["ParID"] = Convert.ToInt32(drs[i]["ParID"]);
                dr["CatRank"] = Convert.ToInt32(drs[i]["CatRank"]);
                dr["SortBy"] = Convert.ToInt32(drs[i]["SortBy"]);
                dr["Num"] = Convert.ToInt32(drs[i]["Num"]);
                dr["CatCode"] = drs[i]["CatCode"].ToString();
                dtRtn.Rows.Add(dr);
                GetCatData(Convert.ToInt32(drs[i]["PKID"]), dtCat, dtRtn);
            }
        }
    }
}